﻿using msBsc.AppLogic.DataAlc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace msBsc.WebUi.ShpMana
{
    public partial class wfScheduleModify : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //保存数据
        public void SaveDataList()
        {

            //获取登录人id
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript("layer.alert('登录已过期，请重新登录', 3);"); return;
            }
            string id = this.GetParamHtValue("id");
            if (string.IsNullOrEmpty(id.Trim()))
            {
                this.ExecJScript("layer.alert('数据错误，请刷新重试', 3);"); return;
            }
            string status = this.GetParamHtValue("status");
            if (string.IsNullOrEmpty(status))
            {
                this.ExecJScript("layer.alert('数据错误，请刷新重试', 3);"); return;
            }
            string vehicleid = this.GetParamHtValue("vehicleid");
            if (string.IsNullOrEmpty(vehicleid))
            {
                this.ExecJScript("layer.alert('数据错误，请刷新重试', 3);"); return;
            }


            string title = this.GetParamHtValue("title");
            string description = this.GetParamHtValue("description");
            string start = this.GetParamHtValue("start");
            string color = this.GetParamHtValue("color");
            //判断车辆是否是个人的，否则不运行修改
            using (CBizVehicleInfo vehicleInfo = new CBizVehicleInfo())
            {
                bool isDjk = vehicleInfo.GetVhicleByUser(userid, vehicleid);
                if (!isDjk)
                {
                    this.ExecJScript("layer.alert('对不起,你没有修改权限', 3);");
                    return;
                }
            }

            //修改 状态
            if (id.Trim() != "999")
            {
                using (CBizVehicleSchedule schedule = new CBizVehicleSchedule())
                {
                    string msg = schedule.UpdateVehicleSchedule(id, Convert.ToInt16(status));
                    if (!string.IsNullOrEmpty(msg))
                    {
                        string json = "{id:\"" + msg + "\",title:\"" + title + "\",description:\"" + description + "\",start:\"" + start.Replace("/", "\\/") + "\",color:\"" + color + "\",status:\"" + status + "\"}";
                        this.ExecJScript("usucccess(" + json + ")");
                        return;
                    }

                    this.ExecJScript("layer.alert('数据错误，请刷新重试', 3);");
                    return;
                }
            }
            //添加
            using (CBizVehicleSchedule schedule = new CBizVehicleSchedule())
            {

                string[] msg = schedule.AddVehicleSchedule(vehicleid, title, description, start, "", color, userid, Convert.ToInt16(status));
                if (Convert.ToBoolean(msg[0]))
                {

                    string json = "{id:\"" + msg[1] + "\",title:\"" + title + "\",description:\"" + description + "\",start:\"" + start.Replace("/", "\\/") + "\",color:\"" + color + "\",status:\"" + status + "\"}";
                    this.ExecJScript(" success(" + json + ");");
                    return;
                }

                this.ExecJScript("layer.alert('" + msg[1] + "', 3);");
            }
        }

        /// <summary>
        /// 加载车辆信息
        /// </summary>
        public void LodData()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript("layer.alert('登录已过期，请重新登录', 3);"); return;
            }
            string vehicleid = this.GetParamHtValue("vehicleid");
            if (string.IsNullOrEmpty(vehicleid))
            {
                this.ExecJScript("layer.alert('数据错误，请刷新重试', 3);"); return;
            }
            
            using (CBizVehicleInfo vehicleInfo = new CBizVehicleInfo())
            {

                DataSet ds = vehicleInfo.GetVehicleInfo(vehicleid);

                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    string name = ds.Tables[0].Rows[0]["PRODUCT_NAME"].ToString();
                    this.ExecJScript("$('#carName').html('" + name + "_车辆日程设置');");
                }
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "save")
            {
                SaveDataList();
            }
            else if (this.strAction == "loding")
            {
                LodData();
            }
        }
    }
}